import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/table';
import { setIsShelf } from '@apps/ant-bus/src/views/business/goods/goods/api';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { render } from '@apps/antd-admin/src/hooks/render/renderUtils';
import { rules } from '@pkg/utils';
import { weChatList } from '@apps/ant-bus/src/views/business/platformManage/wechat/data/api';
import { RenderType } from '@pkg/setting';

export const columns: BasicColumn[] = [
  {
    title: '管家服务名称',
    dataIndex: 'goodsName',
    width: 180,
  },
  {
    title: '安检前后',
    dataIndex: 'securityType',
    width: 180,
    dictData: [
      { label: '安检前', value: 1 },
      { label: '安检后', value: 2 },
    ],
    renderType: RenderType.DICT,
  },
  {
    title: '营业开始时间',
    dataIndex: 'serviceStartTime',
    width: 120,
  },
  {
    title: '营业结束时间',
    dataIndex: 'serviceEndTime',
    width: 120,
  },
  {
    title: '市场价格(元)',
    dataIndex: 'goodsPrice',
    width: 100,
  },
  {
    title: '会员价格(元)',
    dataIndex: 'vipPrice',
    width: 100,
  },
  {
    title: '服务封面图片',
    dataIndex: 'coverImg',
    width: 100,
    customRender: render.renderImage,
  },
  {
    title: '上下架',
    dataIndex: 'isShelf',
    width: 100,
    customRender: ({ record }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.isShelf,
        checkedValue: 1,
        unCheckedValue: 2,
        checkedChildren: '已上架',
        unCheckedChildren: '已下架',
        loading: record.pendingStatus,
        onChange(checked: number) {
          console.info(checked);
          record.pendingStatus = false;
          setIsShelf(record.butlerServiceId, checked)
            .then(data => {
              record.isShelf = checked;
            })
            .catch(data => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '服务地址',
    dataIndex: 'serviceAddress',
    width: 150,
  },
  {
    title: '排序',
    dataIndex: 'goodsSort',
    width: 80,
  },
  {
    title: '创建日期',
    dataIndex: 'createTime',
    width: 150,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'goodsName',
    label: '服务名称',
    component: 'Input',
  },
  {
    field: 'isShelf',
    label: '是否上架',
    component: 'Select',
    componentProps: {
      options: [
        { label: '上架', value: 1 },
        { label: '下架', value: 2 },
      ],
    },
  },
  {
    field: 'weChatId',
    label: '上架公众号',
    component: 'ApiSelect',
    componentProps: {
      api: weChatList,
      params: {},
      labelField: 'weChatName',
      valueField: 'weChatId',
      immediate: true,
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '管家服务Id',
    field: 'butlerServiceId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '机场Id',
    field: 'airportId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '首字母',
    field: 'firstLetter',
    component: 'Input',
    required: true,
  },
  {
    label: '管家服务名称',
    field: 'goodsName',
    component: 'Input',
    required: true,
  },
  {
    label: '摘要说明',
    field: 'butlerServiceSummary',
    component: 'InputTextArea',
    required: true,
  },
  {
    label: '电话号码',
    field: 'butlerServicePhone',
    component: 'Input',
    required: false,
  },
  {
    label: '预约控制(小时)',
    field: 'maxTime',
    component: 'InputNumber',
    required: true,
  },
  /* {
    label: '退单说明(不必填)',
    field: 'refundDesc',
    component: 'InputTextArea',
    required: false,
  },*/
  {
    label: '服务开始时间',
    field: 'serviceStartTime',
    component: 'TimePicker',
    required: true,
    componentProps: {
      valueFormat: 'HH:mm',
      format: 'HH:mm',
    },
  },
  {
    label: '服务结束时间',
    field: 'serviceEndTime',
    component: 'TimePicker',
    required: true,
    componentProps: {
      valueFormat: 'HH:mm',
      format: 'HH:mm',
    },
  },
  {
    label: '市场价格',
    field: 'goodsPrice',
    component: 'InputNumber',
    defaultValue: 0,
    required: true,
    dynamicRules: () => rules.price(true),
  },
  {
    label: '会员价格',
    field: 'vipPrice',
    defaultValue: 0,
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.price(true),
  },
  {
    label: '服务地址',
    field: 'serviceAddress',
    component: 'Input',
    required: true,
  },
  {
    label: '安检前后',
    field: 'securityType',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        { label: '安检前', value: 1 },
        { label: '安检后', value: 2 },
      ],
    },
  },
  {
    label: '封面图',
    field: 'coverImg',
    component: 'ImageUpload',
    required: false,
  },
  {
    label: '轮播图',
    field: 'goodsImg',
    component: 'ImageUpload',
    required: false,
    componentProps: {
      fileMax: 5,
    },
  },
  {
    label: '销量',
    field: 'sales',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.digits(true),
  },
  {
    label: '库存',
    field: 'inventory',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.digits(true),
  },
  {
    label: '排序',
    field: 'goodsSort',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.digits(true),
  },
  {
    label: '服务简介',
    field: 'goodsDesc',
    component: 'TyEditor',
  },
  {
    label: '是否上架',
    field: 'isShelf',
    component: 'Select',
    required: true,
    defaultValue: 2,
    dynamicDisabled: true,
    componentProps: {
      options: [
        { label: '不上架', value: 2 },
        { label: '上架', value: 1 },
      ],
    },
  },
];
